﻿using System;
using AccessControl.Contracts.Models;
using AccessControl.Contracts.Settings;
using AccessControl.Data.Models;

namespace AccessControl.Wpf.Settings.ViewModels
{
	class EntriesSettingsViewModel : SettingsBaseViewModel<IEntry>
	{
		#region Constructors

		public EntriesSettingsViewModel(ISettingsService settingsService) : 
			base(settingsService.Entries)
		{
			Name = "Entries";
		}

		#endregion

		#region SettingsBaseViewModel Overrides
		
		public override void AddItem(IEntry item)
		{
			SettingsService.Add(item);
		}

		public override void EditItem(IEntry item)
		{
			SettingsService.Update(item);
		}

		public override void DeleteItem(IEntry item)
		{
			SettingsService.Delete(item);
		}

		public override bool IsValidItem(IEntry item)
		{
			if (item.Ip == null)
			{
				MessagingService.Show("IP is not valid", "AccessControl", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
				return false;
			}

			if (item.PortNumber <= 0)
			{
				MessagingService.Show("Port number is not valid", "AccessControl", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation);
				return false;
			}

			return true;
		}

		public override IEntry CreateNewItem()
		{
			return new Entry()
			{
				Name = string.Format("New entry({0})", SettingsService.Items.Count + 1)
			};
		}

		#endregion
	}
}
